package org.alexsotob.roo.addon.flyway;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.springframework.roo.shell.CliAvailabilityIndicator;
import org.springframework.roo.shell.CliCommand;
import org.springframework.roo.shell.CliOption;
import org.springframework.roo.shell.CommandMarker;

@Component
// Use these Apache Felix annotations to register your commands class in the Roo
// container
@Service
public class FlywayCommands implements CommandMarker { // All command types must
														// implement the
														// CommandMarker
														// interface

	/**
	 * Get a reference to the Flyway Operations from the underlying OSGi
	 * container
	 */
	@Reference
	private FlywayOperations operations;

	@Reference
	private FlywayMavenOperations mavenOperations;

	@CliAvailabilityIndicator("flyway maven setup")
	public boolean isFlywayMavenPluginInstallationAvailable() {
		return this.mavenOperations.isFlywayMavenInstallationPossible();
	}

	@CliCommand(value = "flyway maven setup", help = "Install Flyway maven plugin")
	public void installFlywayMavenPlugin() {
		this.mavenOperations.configureFlywayMavenPlugin();
	}

	@CliAvailabilityIndicator("flyway maven remove")
	public boolean isFlywayMavenPluginUninstallationAvailable() {
		return this.mavenOperations.isFlywayMavenUnistallationPossible();
	}

	@CliCommand(value = "flyway maven remove", help = "Remove Flyway Maven plugin")
	public void uninstallFlywayMavenPlugin() {
		this.mavenOperations.removeFlywayMavenPlugin();
	}

	@CliAvailabilityIndicator("flyway remove")
	public boolean isFlywayUninstallationAvailable() {
		return this.operations.isFlywayUninstallationPossible();
	}

	@CliCommand(value = "flyway remove", help = "Remove Flyway database migration framework")
	public void uninstallFlyway() {
		this.operations.removeFlyway();
	}

	@CliAvailabilityIndicator("flyway setup")
	public boolean isFlywayInstallationAvailable() {
		return this.operations.isFlywayInstallationPossible();
	}

	@CliCommand(value = "flyway setup", help = "Install Flyway database migration framework")
	public void installFlyway(
			@CliOption(key = "datasourceId", mandatory = false, help = "DataSource bean id to use if it is different than default ones (dataSource).") final String dataSourceId) {
		this.operations.configureFlyway(dataSourceId);
	}

	@CliAvailabilityIndicator("flyway add sql")
	public boolean isNewPlainSqlFileCreationAvailable() {
		return this.operations.isNewPlainSqlFileCreationPossible();
	}

	@CliCommand(value = "flyway add sql", help = "Add new Sql migration file")
	public void addNewSqlMigrationFile(
			@CliOption(key = "version", mandatory = true, help = "Version of file") final String version,
			@CliOption(key = "description", mandatory = false, help = "Description of change") final String description) {
		this.operations.newPlainSqlFile(version, description);
	}

	@CliAvailabilityIndicator("flyway add java")
	public boolean isNewJavaMigrationClassCreationAvailable() {
		return this.operations.isNewJavaMigrationClassCreationPossible();
	}

	@CliCommand(value = "flyway add java", help = "Add new Java migration class")
	public void addNewJavaMigrationClass(
			@CliOption(key = "version", mandatory = true, help = "Version of file") final String version,
			@CliOption(key = "description", mandatory = false, help = "Description of change") final String description) {
		this.operations.newJavaMigrationClass(version, description);
	}

}